package com.cuit.freshmanMS.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.freshmanMS.domain.Academy;
import com.cuit.freshmanMS.domain.Class_;
import com.cuit.freshmanMS.mapper.AcademyMapper;
import com.cuit.freshmanMS.mapper.ClassMapper;
import com.cuit.freshmanMS.query.AcademyQuery;
import com.cuit.freshmanMS.service.AcademyService;
import com.cuit.freshmanMS.util.StringUtil;
import com.cuit.freshmanMS.util.result.CommonResult;
import com.cuit.freshmanMS.util.result.PageResult;
import com.cuit.freshmanMS.util.result.ResultCode;
import com.cuit.freshmanMS.util.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AcademyServiceImpl implements AcademyService {
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private ClassMapper classMapper;
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Academy> loadAll() {
        return academyMapper.loadAll();
    }

    /**
     * 根据ano删除
     * @param ano
     */
    @Override
    public CommonResult deleteAcaByAno(Integer ano) {
        if (ano == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        boolean exists = academyMapper.exists(new QueryWrapper<>(new Academy(ano,null, null,null)));
        if (!exists){
            return ResultUtil.fail(ResultCode.NO_ACA);
        }

        Class_ temp = new Class_();
        temp.setAno(ano);

        boolean classExists = classMapper.exists(new QueryWrapper<>(temp));
        if (classExists){
            return ResultUtil.fail(ResultCode.EXIST_CLASS_IN_ACA);
        }
        academyMapper.deleteById(ano);
        return ResultUtil.success();
    }

    /**
     * 批量删除
     * @param anos
     */
    @Override
    public CommonResult batchDelete(List<Integer> anos) {
        Integer count = academyMapper.deleteBatchIds(anos);
        return ResultUtil.success(count);
    }
    /**
     * 新增aca
     * @param academy
     */
    @Override
    public CommonResult addAca(Academy academy) {

        if(academy.getClass()==null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if(academy.getTime()==null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtil.isNull(academy.getAname(), academy.getPlace())) {
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        academyMapper.insert(academy);
        return ResultUtil.success();
    }

    /**
     * 更新
     * @param academy
     */
    @Override
    public CommonResult updateAca(Academy academy) {
        // 错误检测
        if (academy.getAno() == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtil.isNull(academy.getAname(), academy.getPlace(), academy.getTime().toString(), academy.getClass().toString())){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        academyMapper.updateById(academy);
        return ResultUtil.success();
    }

    /**
     * 分页查询
     * @param academyQuery
     * @return
     */
    @Override
    public PageResult<Academy> selectAll(AcademyQuery academyQuery) {
        if (academyQuery.getCurrentPage()== null || academyQuery.getPageSize() == null) {
            return new PageResult<>();
        }

        if (academyQuery.getCurrentPage() <= 0 || academyQuery.getPageSize() <= 0) {
            return new PageResult<>();
        }

        QueryWrapper<Academy> queryWrapper = new QueryWrapper<>(new Academy()).like("aname", academyQuery.getAnameK())
                .like("place", academyQuery.getPlaceK());
        if (academyQuery.getTimeBegin() != null && academyQuery.getTimeEnd() != null) {
            queryWrapper.between("time", academyQuery.getTimeBegin(), academyQuery.getTimeEnd());
        }
        if (academyQuery.getTimeBegin() != null && academyQuery.getTimeEnd() == null) {
            queryWrapper.ge("time", academyQuery.getTimeBegin());
        }
        if (academyQuery.getTimeBegin() == null && academyQuery.getTimeEnd() != null) {
            queryWrapper.le("time", academyQuery.getTimeEnd());
        }
        Page<Academy> resultPage = academyMapper.selectPage(
                new Page<>(academyQuery.getCurrentPage(), academyQuery.getPageSize()),queryWrapper);

        return new PageResult<>(resultPage);
    }

    @Override
    public CommonResult<Academy> getAcaByAno(Integer ano) {
        if (ano == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        return ResultUtil.success(academyMapper.selectById(ano));
    }
}



